/*
 * Jooby https://jooby.io
 * Apache License Version 2.0 https://jooby.io/LICENSE.txt
 * Copyright 2014 Edgar Espina
 */
package issues.i3729.api;

import static org.junit.jupiter.api.Assertions.assertEquals;

import io.jooby.openapi.OpenAPIResult;
import io.jooby.openapi.OpenAPITest;

public class ApiDocTest {

  @OpenAPITest(value = AppLibrary.class)
  public void shouldGenerateMvcDoc(OpenAPIResult result) {
    checkResult(result);
  }

  @OpenAPITest(value = ScriptLibrary.class)
  public void shouldGenerateScriptDoc(OpenAPIResult result) {
    checkResult(result);
  }

  private void checkResult(OpenAPIResult result) {
    assertEquals(
        "openapi: 3.0.1\n"
            + "info:\n"
            + "  title: Library API.\n"
            + "  description: \"Available data: Books and authors.\"\n"
            + "  contact:\n"
            + "    name: Jooby\n"
            + "    url: https://jooby.io\n"
            + "    email: support@jooby.io\n"
            + "  license:\n"
            + "    name: Apache\n"
            + "    url: https://jooby.io/LICENSE\n"
            + "  version: 4.0.0\n"
            + "  x-logo:\n"
            + "    url: https://redocly.github.io/redoc/museum-logo.png\n"
            + "    altText: Museum logo\n"
            + "servers:\n"
            + "- url: https://api.fake-museum-example.com/v1\n"
            + "tags:\n"
            + "- name: Library\n"
            + "  description: Access to all books.\n"
            + "- name: Author\n"
            + "  description: Oxxx\n"
            + "paths:\n"
            + "  /api/library/{isbn}:\n"
            + "    summary: Library API.\n"
            + "    description: \"Contains all operations for creating, updating and fetching"
            + " books.\"\n"
            + "    get:\n"
            + "      tags:\n"
            + "      - Library\n"
            + "      - Book\n"
            + "      - Author\n"
            + "      summary: Find a book by isbn.\n"
            + "      operationId: bookByIsbn\n"
            + "      parameters:\n"
            + "      - name: isbn\n"
            + "        in: path\n"
            + "        description: Book isbn. Like IK-1900.\n"
            + "        required: true\n"
            + "        schema:\n"
            + "          type: string\n"
            + "      responses:\n"
            + "        \"200\":\n"
            + "          description: A matching book.\n"
            + "          content:\n"
            + "            application/json:\n"
            + "              schema:\n"
            + "                $ref: \"#/components/schemas/Book\"\n"
            + "        \"404\":\n"
            + "          description: \"Not Found: If a book doesn't exist.\"\n"
            + "        \"400\":\n"
            + "          description: \"Bad Request: For bad ISBN code.\"\n"
            + "  /api/library/{id}:\n"
            + "    summary: Library API.\n"
            + "    description: \"Contains all operations for creating, updating and fetching"
            + " books.\"\n"
            + "    get:\n"
            + "      tags:\n"
            + "      - Library\n"
            + "      - Author\n"
            + "      summary: Author by Id.\n"
            + "      operationId: author\n"
            + "      parameters:\n"
            + "      - name: id\n"
            + "        in: path\n"
            + "        description: ID.\n"
            + "        required: true\n"
            + "        schema:\n"
            + "          type: string\n"
            + "      responses:\n"
            + "        \"200\":\n"
            + "          description: An author\n"
            + "          content:\n"
            + "            application/json:\n"
            + "              schema:\n"
            + "                $ref: \"#/components/schemas/Author\"\n"
            + "  /api/library:\n"
            + "    summary: Library API.\n"
            + "    description: \"Contains all operations for creating, updating and fetching"
            + " books.\"\n"
            + "    get:\n"
            + "      tags:\n"
            + "      - Library\n"
            + "      summary: Query books.\n"
            + "      operationId: query\n"
            + "      parameters:\n"
            + "      - name: title\n"
            + "        in: query\n"
            + "        description: Book's title.\n"
            + "        schema:\n"
            + "          type: string\n"
            + "      - name: author\n"
            + "        in: query\n"
            + "        description: Book's author. Optional.\n"
            + "        schema:\n"
            + "          type: string\n"
            + "      - name: isbn\n"
            + "        in: query\n"
            + "        description: Book's isbn. Optional.\n"
            + "        schema:\n"
            + "          type: string\n"
            + "      responses:\n"
            + "        \"200\":\n"
            + "          description: Matching books.\n"
            + "          content:\n"
            + "            application/json:\n"
            + "              schema:\n"
            + "                type: array\n"
            + "                items:\n"
            + "                  $ref: \"#/components/schemas/Book\"\n"
            + "      x-badges:\n"
            + "      - name: Beta\n"
            + "        position: before\n"
            + "        color: purple\n"
            + "    post:\n"
            + "      tags:\n"
            + "      - Library\n"
            + "      - Author\n"
            + "      summary: Creates a new book.\n"
            + "      description: Book can be created or updated.\n"
            + "      operationId: createBook\n"
            + "      requestBody:\n"
            + "        description: Book to create.\n"
            + "        content:\n"
            + "          application/json:\n"
            + "            schema:\n"
            + "              $ref: \"#/components/schemas/Book\"\n"
            + "            example:\n"
            + "              isbn: X01981\n"
            + "              title: HarryPotter\n"
            + "        required: true\n"
            + "      responses:\n"
            + "        \"200\":\n"
            + "          description: Saved book.\n"
            + "          content:\n"
            + "            application/json:\n"
            + "              schema:\n"
            + "                $ref: \"#/components/schemas/Book\"\n"
            + "              example:\n"
            + "                id: generatedId\n"
            + "                isbn: '...'\n"
            + "components:\n"
            + "  schemas:\n"
            + "    Author:\n"
            + "      type: object\n"
            + "      properties:\n"
            + "        ssn:\n"
            + "          type: string\n"
            + "          description: Social security number.\n"
            + "        name:\n"
            + "          type: string\n"
            + "          description: Author's name.\n"
            + "        address:\n"
            + "          $ref: \"#/components/schemas/Address\"\n"
            + "        books:\n"
            + "          uniqueItems: true\n"
            + "          type: array\n"
            + "          description: Published books.\n"
            + "          items:\n"
            + "            $ref: \"#/components/schemas/Book\"\n"
            + "    BookQuery:\n"
            + "      type: object\n"
            + "      properties:\n"
            + "        title:\n"
            + "          type: string\n"
            + "          description: Book's title.\n"
            + "        author:\n"
            + "          type: string\n"
            + "          description: Book's author. Optional.\n"
            + "        isbn:\n"
            + "          type: string\n"
            + "          description: Book's isbn. Optional.\n"
            + "      description: Query books by complex filters.\n"
            + "    Address:\n"
            + "      type: object\n"
            + "      properties:\n"
            + "        street:\n"
            + "          type: string\n"
            + "          description: Street name.\n"
            + "        city:\n"
            + "          type: string\n"
            + "          description: City name.\n"
            + "        state:\n"
            + "          type: string\n"
            + "          description: State.\n"
            + "        country:\n"
            + "          type: string\n"
            + "          description: Two digit country code.\n"
            + "      description: Author address.\n"
            + "    Book:\n"
            + "      type: object\n"
            + "      properties:\n"
            + "        isbn:\n"
            + "          type: string\n"
            + "          description: Book ISBN. Method.\n"
            + "        title:\n"
            + "          type: string\n"
            + "          description: Book's title.\n"
            + "        publicationDate:\n"
            + "          type: string\n"
            + "          description: Publication date. Format mm-dd-yyyy.\n"
            + "          format: date\n"
            + "        text:\n"
            + "          type: string\n"
            + "        type:\n"
            + "          type: string\n"
            + "          description: |-\n"
            + "            Book type.\n"
            + "              - Fiction: Fiction books are based on imaginary characters and events,"
            + " while non-fiction books are based o n real people and events.\n"
            + "              - NonFiction: Non-fiction genres include biography, autobiography,"
            + " history, self-help, and true crime.\n"
            + "          enum:\n"
            + "          - Fiction\n"
            + "          - NonFiction\n"
            + "        authors:\n"
            + "          uniqueItems: true\n"
            + "          type: array\n"
            + "          items:\n"
            + "            $ref: \"#/components/schemas/Author\"\n"
            + "      description: Book model.\n",
        result.toYaml());
  }
}
